package com.hangyutech.strelitzia.utils;

import com.alibaba.druid.support.http.util.IPAddress;

import javax.servlet.http.HttpServletRequest;

public class HttpUtil {


    public static String getIpAddress(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip.equals("0:0:0:0:0:0:0:1") ? "127.0.0.1" : ip;
    }

    public static Integer getIpAddressNumber(HttpServletRequest request) {
        IPAddress ip = new IPAddress(HttpUtil.getIpAddress(request));
        return ip.getIPAddress();
    }
}
